package cn.jy.nvr.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.LC_BASE_LCBLFY;
import cn.jy.nvr.domain.PredictPlanTask;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.ProviderCollection;
import cn.jy.nvr.domain.XY_BASE_FIVERELDONNUR;
import cn.jy.nvr.domain.XY_BASE_RELDONNUR;
import cn.jy.nvr.mapper.LC_BASE_LCBLFYMapper;
import cn.jy.nvr.mapper.PredictPlanTaskMapper;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.ProviderCollectionMapper;
import cn.jy.nvr.mapper.XY_BASE_FIVERELDONNURMapper;
import cn.jy.nvr.mapper.XY_BASE_RELDONNURMapper;
import cn.jy.nvr.service.PredictPlanTaskQuartzJob;
import cn.jy.nvr.utils.DateTools;
/**
 * @author Administrator
 * 该定时任务用于统计浆员电话微信   五步链接的
 */
@Service("PredictPlanTaskQuartzJob")
public class PredictPlanTaskQuartzJobImpl implements PredictPlanTaskQuartzJob{
     @Autowired
     ProviderBaseInfoMapper providerBaseInfoMapper;
     @Autowired 
     PredictPlanTaskMapper predictPlanTaskMapper;
     @Autowired 
     ProviderCollectionMapper  collectionMapper;
     @Autowired
     XY_BASE_RELDONNURMapper xy_BASE_RELDONNURMapper;
     @Autowired 
     LC_BASE_LCBLFYMapper   lc_BASE_LCBLFYMapper;
     @Autowired
     XY_BASE_FIVERELDONNURMapper xy_BASE_FIVERELDONNURMapper;
     Logger logger=Logger.getLogger(PredictPlanTaskQuartzJobImpl.class);
	@Override
	public void work() {
		//先去查询当日是否有新浆员如果有就给他放到预测计划表里
		String nowDate = DateTools.getInstance().dateFormat(new Date(), "yyyy-MM-dd");
		List<ProviderBaseInfo> nowDayproviderBaseInfoList = providerBaseInfoMapper.getProviderBaseInfoByCreateRQ(nowDate);
		if (nowDayproviderBaseInfoList.size()>0) {
			for (ProviderBaseInfo providerBaseInfo : nowDayproviderBaseInfoList) {
			       PredictPlanTask predictPlanTask = new PredictPlanTask();
			       predictPlanTask.setCARDID(providerBaseInfo.getCARDID());
			       predictPlanTask.setCARDID(providerBaseInfo.getID());
			       predictPlanTask.setID(UUID.randomUUID().toString());
			       logger.info("今日新浆员添加入计划"+providerBaseInfo.getID());
			       if(!providerBaseInfo.getID().startsWith("R")){
			    	   predictPlanTaskMapper.savePredictPlanTask(predictPlanTask);
			       }
		}
		}
		
		List<PredictPlanTask> predictPlanTaskList = predictPlanTaskMapper.loadAll();
		
		for (PredictPlanTask predictPlanTask : predictPlanTaskList) {
			    //查询上一次献浆时间
			if (predictPlanTask!=null&&predictPlanTask.getCARDID()!=null&&!predictPlanTask.getCARDID().startsWith("R")) {
				//查询护士的基本信息
				ProviderBaseInfo providerBaseInfoByCardId = providerBaseInfoMapper.getProviderBaseInfoByCardId(predictPlanTask.getCARDID());
				
				DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
				List<ProviderCollection> loadProviderCollections = collectionMapper.loadProviderCollections(predictPlanTask.getCARDID());
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				logger.info("更新浆员入计划    卡号:"+predictPlanTask.getCARDID()+"一共采浆"+loadProviderCollections.size());
				
				
				if (loadProviderCollections.size()==0) {
					predictPlanTask.setISCOMING("0");
				}
				
				if (loadProviderCollections.size()>0) {
					//上一次采浆时间
					String lastCollectionDate = loadProviderCollections.get(0).getEND_TIME();
					predictPlanTask.setLASTCOLLECTIONDATE(lastCollectionDate);
					boolean isComing = lastCollectionDate.startsWith(nowDate.substring(0,4));
					//采浆时间是今年的今年已经进站
					if (isComing) {
						predictPlanTask.setISCOMING("1");
					}else {
						predictPlanTask.setISCOMING("0");
					}
					//判断是否有微信号
					if(predictPlanTask.getJYXM()!=null){
						XY_BASE_RELDONNUR loadXY_BASE_RELDONNURByJYID = xy_BASE_RELDONNURMapper.loadXY_BASE_RELDONNURByJYID(predictPlanTask.getJYXM());
						if(loadXY_BASE_RELDONNURByJYID!=null&&"1".equals(loadXY_BASE_RELDONNURByJYID.getSFSYWX())){
							predictPlanTask.setFOCUSWEIXIN("0");
						}else {
							predictPlanTask.setFOCUSWEIXIN("1");
						}
					//设置链接护士
                       if(loadXY_BASE_RELDONNURByJYID!=null&&loadXY_BASE_RELDONNURByJYID.getHSXM()!=null){
                    	   predictPlanTask.setLJHS(loadXY_BASE_RELDONNURByJYID.getHSXM());
						}
						//临床反应的
						 LC_BASE_LCBLFY lC_BASE_LCBLFY=new LC_BASE_LCBLFY();
						 lC_BASE_LCBLFY.setJYXM(predictPlanTask.getJYXM());
						 List<LC_BASE_LCBLFY> loadLC_BASE_LCBLFYList = lc_BASE_LCBLFYMapper.loadLC_BASE_LCBLFY(lC_BASE_LCBLFY);
					    if(loadLC_BASE_LCBLFYList.size()>0){
					    	LC_BASE_LCBLFY loadLC_BASE_LCBLFY = loadLC_BASE_LCBLFYList.get(0);
					    	if (loadLC_BASE_LCBLFY!=null&&loadLC_BASE_LCBLFY.getJYXM()!=null) {
								predictPlanTask.setSPECIALSERVICE("1");
								predictPlanTask.setLCFYRQ(loadLC_BASE_LCBLFY.getFSRQ());
							}
					    }	 
						
					}
				}
				
				
				//根据卡号查询基本信息里面是否有电话号码？
				if(providerBaseInfoByCardId!=null &&providerBaseInfoByCardId.getPHONE()==null){
					predictPlanTask.setCOLLECTPHONE("1");	
				}
				
                //小于4次五步链接
				
				if (providerBaseInfoByCardId!=null &&providerBaseInfoByCardId.getCREATERQ()!=null) {
					String createRQ = providerBaseInfoByCardId.getCREATERQ();
					   Calendar c = Calendar.getInstance();
					    c.setTime(new Date());
					    String nowYear=   String.valueOf(c.get(Calendar.YEAR)) ;
					    c.add(Calendar.YEAR, -1);
					    String lastYear=   String.valueOf(c.get(Calendar.YEAR)) ;
					    //今年建档的算是进站的
					    if (createRQ.startsWith(nowYear)) {
					    	predictPlanTask.setISCOMING("1");
						}
					    
					    if (createRQ.startsWith(nowYear)||createRQ.startsWith(lastYear)) {
					    	
					    	if (loadProviderCollections.size()<4) {
					    		predictPlanTask.setFIVELINK("1");
					    		predictPlanTask.setCOLLECTIONNUMS(String.valueOf(loadProviderCollections.size()));
					    		
					    		
					    		XY_BASE_FIVERELDONNUR xY_BASE_FIVERELDONNUR=new XY_BASE_FIVERELDONNUR();
						    	xY_BASE_FIVERELDONNUR.setJYXM(predictPlanTask.getJYXM());
						    	DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
								List<XY_BASE_FIVERELDONNUR> fiveLinkList = xy_BASE_FIVERELDONNURMapper.loadXY_BASE_FIVERELDONNUR(xY_BASE_FIVERELDONNUR);
								List<String> rqList= new ArrayList<String>();
								 if (fiveLinkList.size()>0) {
									for (XY_BASE_FIVERELDONNUR xy_BASE_FIVERELDONNUR2 : fiveLinkList) {
										if (rqList.size()==0&&xy_BASE_FIVERELDONNUR2.getLJTIME()!=null) {
											rqList.add(xy_BASE_FIVERELDONNUR2.getLJTIME().substring(0, 10));
										}
										if (rqList.size()>0&&xy_BASE_FIVERELDONNUR2.getLJTIME()!=null) {
											
											int temp=0;
											
											for (int i = 0; i < rqList.size(); i++) {
												String string = rqList.get(i);
												boolean startsWith = xy_BASE_FIVERELDONNUR2.getLJTIME().startsWith(string);
												if(startsWith){
													temp++;
												 }
											}
											if (temp==0) {
												rqList.add(xy_BASE_FIVERELDONNUR2.getLJTIME().substring(0, 10));
											}
											

										}
									}
									 int rqsize = rqList.size(); 
									 if (rqsize>0) {
								    	 predictPlanTask.setFIVELINKNUMS(String.valueOf(rqsize));
									}
								} 
								
					    	}
					    	
							 
						}
				}
				   
				    
				//更新数据
				predictPlanTaskMapper.updatePredictPlanTask(predictPlanTask);
				
			}
		    
			    
		}
		
	}
}
